<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace AlibabaCloud\Client\Exception;

class ServerException extends AlibabaCloudException
{
    protected $requestId;
    protected $result;
    public function __construct(\AlibabaCloud\Client\Result\Result $result, $errorMessage = \AlibabaCloud\Client\SDK::RESPONSE_EMPTY, $errorCode = \AlibabaCloud\Client\SDK::SERVICE_UNKNOWN_ERROR)
    {
        $this->result = $result;
        $this->errorMessage = $errorMessage;
        $this->errorCode = $errorCode;
        $this->resolvePropertiesByReturn();
        $this->distinguishSignatureErrors();
        $this->bodyAsErrorMessage();
        parent::__construct($this->getMessageString(), $this->result->getStatusCode());
    }
    private function resolvePropertiesByReturn()
    {
        if(isset($this->result["message"])) {
            $this->errorMessage = $this->result["message"];
            $this->errorCode = $this->result["code"];
        }
        if(isset($this->result["Message"])) {
            $this->errorMessage = $this->result["Message"];
            $this->errorCode = $this->result["Code"];
        }
        if(isset($this->result["errorMsg"])) {
            $this->errorMessage = $this->result["errorMsg"];
            $this->errorCode = $this->result["errorCode"];
        }
        if(isset($this->result["requestId"])) {
            $this->requestId = $this->result["requestId"];
        }
        if(isset($this->result["RequestId"])) {
            $this->requestId = $this->result["RequestId"];
        }
    }
    private function distinguishSignatureErrors()
    {
        if($this->result->getRequest() && \Stringy\Stringy::create($this->errorMessage)->contains($this->result->getRequest()->stringToSign())) {
            $this->errorCode = "InvalidAccessKeySecret";
            $this->errorMessage = "Specified Access Key Secret is not valid.";
        }
    }
    private function bodyAsErrorMessage()
    {
        $body = (string) $this->result->getBody();
        if($this->errorMessage === \AlibabaCloud\Client\SDK::RESPONSE_EMPTY && $body) {
            $this->errorMessage = $body;
        }
    }
    private function getMessageString()
    {
        $message = $this->errorCode . ": " . $this->errorMessage . " RequestId: " . $this->requestId;
        if($this->getResult()->getRequest()) {
            $method = $this->getResult()->getRequest()->method;
            $uri = (string) $this->getResult()->getRequest()->uri;
            $message .= " " . $method . " \"" . $uri . "\"";
            if($this->result) {
                $message .= " " . $this->result->getStatusCode();
            }
        }
        return $message;
    }
    public function getResult()
    {
        return $this->result;
    }
    public function getRequestId()
    {
        return $this->requestId;
    }
    public function getErrorType()
    {
        return "Server";
    }
    public function getHttpStatus()
    {
        return $this->getResult()->getStatusCode();
    }
}

?>